/**
 * File Comment: Ita a GUI
 */
package edu.gatech.oad.project.presentation;
//$codepro.audit.disable
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;

/**
 * This class is a panel that allows the user
 * to interact with the GUI to sign up for an
 * account with the system.
 * 
 * @author Group 17
 * @version 1
 */
public class SignUp extends JPanel {
  
  /**
   * Field driver: holds the Driver connected to the GUI
   */
  private Driver driver;
  
  /**
   * Field lblRequestMembershipe: label
   */
  public JLabel lblRequestMembership;
  
  /**
   * Field lblDesiredUserName: label
   */
  public JLabel lblDesiredUserName;
  
  /**
   * Field lblDesiredPassword: label
   */
  public JLabel lblDesiredPassword;
  
  /**
   * Field lblConfirmPassword: label
   */
  public JLabel lblConfirmPassword;
  
  /**
   * Field lblUserType: label
   */
  public JLabel lblUserType;
  
  /**
   * Field comboBox: options for user type
   */
  public JComboBox comboBox;
  
  /**
   * Field btnSignUp: sign up button
   */
  public JButton btnSignUp;
  
  /**
   * Field btnBackToLog: back to log in button
   */
  public JButton btnBackToLog;
  
  /**
   * Field txtUser: username field
   */
 private JTextField txtUser;
 
 /**
  * Field passwordField: password field
  */
 private JPasswordField passwordField;
 
 /**
  * Field passwordField_1: confirm password field
  */
 private JPasswordField passwordField_1;
 
 /**
  * Field username: desired username
  */
 private String username;
 
 /**
  * Field password1: desired password
  */
 private char[] password1;
 
 /**
  * Field password2: confirm password
  */
 private char[] password2;
 
 /**
  * Field userType: integer representing the type of account the user is requesting
  */
 private int userType;

 /**
  * Constructor.
  * 
  * @param driver the Driver connected to the GUI
  */
 public SignUp(Driver driver) {
   this.driver = driver;
  GridBagLayout gridBagLayout = new GridBagLayout();
  gridBagLayout.columnWidths = new int[]{0, 0, 0, 100, 141, 0};
  gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  gridBagLayout.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
  gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
  setLayout(gridBagLayout);
  
  lblRequestMembership = new JLabel("Request Membership");
  lblRequestMembership.setFont(new Font("Lucida Grande", Font.PLAIN, 30));
  GridBagConstraints gbc_lblRequestMembership = new GridBagConstraints();
  gbc_lblRequestMembership.gridwidth = 5;
  gbc_lblRequestMembership.insets = new Insets(0, 0, 5, 0);
  gbc_lblRequestMembership.gridx = 1;
  gbc_lblRequestMembership.gridy = 1;
  add(lblRequestMembership, gbc_lblRequestMembership);
  
  lblDesiredUserName = new JLabel("Desired User name:");
  GridBagConstraints gbc_lblDesiredUserName = new GridBagConstraints();
  gbc_lblDesiredUserName.anchor = GridBagConstraints.EAST;
  gbc_lblDesiredUserName.insets = new Insets(0, 0, 5, 5);
  gbc_lblDesiredUserName.gridx = 1;
  gbc_lblDesiredUserName.gridy = 3;
  add(lblDesiredUserName, gbc_lblDesiredUserName);
  
  txtUser = new JTextField(10);
  txtUser.addCaretListener(new CaretListener() {
    public void caretUpdate(CaretEvent e) {
      username = txtUser.getText();
    }
  });
  GridBagConstraints gbc_txtUser = new GridBagConstraints();
  gbc_txtUser.gridwidth = 2;
  gbc_txtUser.insets = new Insets(0, 0, 5, 5);
  gbc_txtUser.fill = GridBagConstraints.HORIZONTAL;
  gbc_txtUser.gridx = 2;
  gbc_txtUser.gridy = 3;
  add(txtUser, gbc_txtUser);
  txtUser.setColumns(10);
  
  lblDesiredPassword = new JLabel("Desired Password:");
  GridBagConstraints gbc_lblDesiredPassword = new GridBagConstraints();
  gbc_lblDesiredPassword.anchor = GridBagConstraints.EAST;
  gbc_lblDesiredPassword.insets = new Insets(0, 0, 5, 5);
  gbc_lblDesiredPassword.gridx = 1;
  gbc_lblDesiredPassword.gridy = 4;
  add(lblDesiredPassword, gbc_lblDesiredPassword);
  
  passwordField_1 = new JPasswordField(10);
  passwordField_1.addCaretListener(new CaretListener() {
    public void caretUpdate(CaretEvent e) {
      password1 = passwordField_1.getPassword();
    }
  });
  GridBagConstraints gbc_passwordField_1 = new GridBagConstraints();
  gbc_passwordField_1.gridwidth = 2;
  gbc_passwordField_1.insets = new Insets(0, 0, 5, 5);
  gbc_passwordField_1.fill = GridBagConstraints.HORIZONTAL;
  gbc_passwordField_1.gridx = 2;
  gbc_passwordField_1.gridy = 4;
  add(passwordField_1, gbc_passwordField_1);
  
  lblConfirmPassword = new JLabel("Confirm Password:");
  GridBagConstraints gbc_lblConfirmPassword = new GridBagConstraints();
  gbc_lblConfirmPassword.anchor = GridBagConstraints.EAST;
  gbc_lblConfirmPassword.insets = new Insets(0, 0, 5, 5);
  gbc_lblConfirmPassword.gridx = 1;
  gbc_lblConfirmPassword.gridy = 5;
  add(lblConfirmPassword, gbc_lblConfirmPassword);
  
  passwordField = new JPasswordField(10);
  passwordField.addCaretListener(new CaretListener() {
    public void caretUpdate(CaretEvent e) {
      password2 = passwordField.getPassword();
    }
  });
  GridBagConstraints gbc_passwordField = new GridBagConstraints();
  gbc_passwordField.gridwidth = 2;
  gbc_passwordField.insets = new Insets(0, 0, 5, 5);
  gbc_passwordField.fill = GridBagConstraints.HORIZONTAL;
  gbc_passwordField.gridx = 2;
  gbc_passwordField.gridy = 5;
  add(passwordField, gbc_passwordField);
  
  lblUserType = new JLabel("User type:");
  GridBagConstraints gbc_lblUserType = new GridBagConstraints();
  gbc_lblUserType.insets = new Insets(0, 0, 5, 5);
  gbc_lblUserType.anchor = GridBagConstraints.EAST;
  gbc_lblUserType.gridx = 1;
  gbc_lblUserType.gridy = 6;
  add(lblUserType, gbc_lblUserType);
  
  String[] userTypes = {"Patient","Nurse","Doctor","System Admin"};
  comboBox = new JComboBox(userTypes);
  comboBox.setSelectedIndex(0);
  comboBox.addActionListener(new ComboBoxListener(this));
  GridBagConstraints gbc_comboBox = new GridBagConstraints();
  gbc_comboBox.gridwidth = 2;
  gbc_comboBox.insets = new Insets(0, 0, 5, 5);
  gbc_comboBox.fill = GridBagConstraints.HORIZONTAL;
  gbc_comboBox.gridx = 2;
  gbc_comboBox.gridy = 6;
  add(comboBox, gbc_comboBox);
  
  btnSignUp = new JButton("Sign up");
  btnSignUp.addActionListener(new ButtonListener1(this));
  GridBagConstraints gbc_btnSignUp = new GridBagConstraints();
  gbc_btnSignUp.insets = new Insets(0, 0, 5, 5);
  gbc_btnSignUp.gridx = 2;
  gbc_btnSignUp.gridy = 7;
  add(btnSignUp, gbc_btnSignUp);
  
  btnBackToLog = new JButton("Return to Log In");
  btnBackToLog.addActionListener(new ButtonListener2(this));
  GridBagConstraints gbc_btnBackToLog = new GridBagConstraints();
  gbc_btnBackToLog.insets = new Insets(0, 0, 5, 5);
  gbc_btnBackToLog.gridx = 3;
  gbc_btnBackToLog.gridy = 7;
  add(btnBackToLog, gbc_btnBackToLog);
 } // end constructor method
 
 /**
  * Get the username.
  * 
  * @return the desired username
  */
 public String getUsername()
 {
   return this.username;
 } // end getUsername method
 
 /**
  * Get the password.
  * 
  * @return the desired password
  */
 public char[] getPassword1()
 {
   return this.password1;
 } // end getPassword1 method
 
 /**
  * Get the password confirmation.
  * 
  * @return the confirmation for the password
  */
 public char[] getPassword2()
 {
   return this.password2;
 } // end getPassword2 method
 
 /**
  * Get the account type.
  * 
  * @return the integer representing the type of account the user is requesting
  */
 public int getUserType()
 {
   return this.userType;
 } // end getUserType method
 
 /**
  * Set the account type.
  * 
  * @param the integer retpresenting the new account type
  */
 public void setUserType(int userType)
 {
   this.userType = userType;
 } // end setUser method
 
 /**
  * Get the Driver.
  * 
  * @return the Driver connected to the GUI
  */
 public Driver getDriver()
 {
   return this.driver;
 } // end getDriver method
 
 /**
  * This helper methodcompares two passwords for verification.
  * 
  * @param p1 the first password
  * @param p2 the second password
  */
 public boolean comparePasswords(char[] p1, char[] p2)
 {
   boolean out = true;
   if (p1.length != p2.length)
   {
     return false;
   }
   else
   {
     for (int x = 0; x < p1.length; x++)
     {
       if(p1[x] != p2[x])
       {
         return false;
       }
     }
   }
   return true;
   
 } // end comparePasswords method
 
 /**
  * This private inner class implements ActionListener for comboBox.
  * 
  * @author Group 17
  * @version 1
  */
 private class ComboBoxListener implements ActionListener
 {
   /**
    * Field signup: the SignUp being used
    */
   private SignUp signup;
   
   /**
    * Constructor.
    * 
    * @param signup the SignUp being used
    */
   private ComboBoxListener(SignUp signup)
   {
     this.signup = signup;
   } // end constructor method
   
   /**
    * Default method for ActionListener.
    * 
    * @param e the ActionEvent triggering the ActionListener
    */
   public void actionPerformed(ActionEvent e)
   {
     String usertype = (String)((JComboBox)e.getSource()).getSelectedItem();
     if(usertype.equals("Patient"))
     {
       this.signup.setUserType(0);
     }
     else if(usertype.equals("Nurse"))
     {
       this.signup.setUserType(1);
     }
     else if(usertype.equals("Doctor"))
     {
       this.signup.setUserType(2);
     }
     else if(usertype.equals("System Admin"))
     {
       this.signup.setUserType(3);
     }
   } // end actionPerformed method
 } // end ComboBoxListener class 
 
 /**
  * This private inner class implements ActionListener for btnSignUp.
  * 
  * @author Group 17
  * @version 1
  */
 private class ButtonListener1 implements ActionListener
 {
   /**
    * Field driver: holds the Driver connected to the GUI
    */
   private Driver driver;
   
   /**
    * Field signup: the SignUp being used
    */
   private SignUp signup;
   
   /**
    * Constructor.
    * 
    * @param signup the SignUp being used
    */
   private ButtonListener1(SignUp signup)
   {
     this.signup = signup;
     this.driver = signup.getDriver();
   } // end constructor method
   
   /**
    * Default method for ActionListener.
    * 
    * @param e the ActionEvent triggering the ActionListener
    */
   public void actionPerformed(ActionEvent e)
   {
     //System.out.println(password1);
     if(this.signup.comparePasswords(this.signup.getPassword1(),this.signup.getPassword2()) == false)
     {
       this.signup.btnSignUp.setText("Passwords do not match.");
     }
     else
     {
       boolean response = DriverMain.registerInSystem(this.signup.getUsername(), 
                 new String(this.signup.getPassword1()), this.signup.getUserType());
       if (response) {
           this.signup.btnSignUp.setText("Request submitted.");
       }
       else {
           this.signup.btnSignUp.setText("Username invalid.");
       }
       
     }
   } // end actionPerformed method
 } // end ButtonListener1 class
 
 /**
  * This private inner class implements ActionListener for btnBackToLog.
  * 
  * @author 17
  * @version 1
  */
 private class ButtonListener2 implements ActionListener
 {
   /**
    * Field driver: holds the Driver connected to the GUI
    */
   private Driver driver;
   
   /**
    * Field signup: the SignUp being used
    */
   private SignUp signup;
   
   /**
    * Constructor.
    * 
    * @param signup the SignUp being used
    */
   private ButtonListener2(SignUp signup)
   {
     this.signup = signup;
     this.driver = signup.getDriver();
   } // end constructor method
   
   /**
    * Default method for ActionListener.
    * 
    * @param e the ActionEvent triggering the ActionListener
    */
   public void actionPerformed(ActionEvent e)
   {
     this.driver.getFrame().setContentPane(new LogIn(this.driver));
     this.driver.getFrame().pack();
   } // end actionPerformed method
 } // end ButtonListener2 class
} // end SignUp class